﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;
using System.Collections;

namespace Joyintech.Admin.inpiao365.general.cinema
{
    /// <summary>
    /// 电影院编辑
    /// </summary>
    public partial class edit : System.Web.UI.Page
    {
        #region 页面加载

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.StartDate.Value = DateTime.Now;
                this.EndDate.Value = DateTime.Now.AddMonths(6);

                //加载城市
                DataBindCity();

                this.ddlCity.Items.Insert(0,new ListItem("==请选择城市==", ""));

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("theater_Id") != "")
                {
                    DataBindForm();
                }
            }
        }

        #endregion

        #region 加载区域

        /// <summary>
        /// 加载城市
        /// </summary>
        private void DataBindCity()
        {
            ddlCity.Items.Clear();

            List<movieCity> list = new List<movieCity>();

            list = new movieCity_BLL().GetModelList("1=1 order by CityOrder");
            
            if (list != null && list.Count > 0)
            {
                foreach (movieCity model in list)
                {
                    ListItem item = new ListItem();

                    item.Text = model.CityName;
                    item.Value = model.CityId.ToString();

                    ddlCity.Items.Add(item);
                }
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new theater_BLL().GetList("TheaterId=" + DNTRequest.GetQueryString("theater_Id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);

                //绑定电影院所属城市和行政区
                movieCity model = new movieCity();

                model = new movieCity_BLL().GetModel(Convert.ToInt32(ds.Tables[0].Rows[0]["CityId"].ToString()));

                if (model != null)
                {
                    this.ddlCity.SelectedValue = model.CityId.ToString();

                    //加载行政区列表
                    this.ddlCity_SelectedIndexChanged(new object(), new EventArgs());

                    //选中行政区
                    this.ddlCityArea.SelectedValue = ds.Tables[0].Rows[0]["AreaId"].ToString();
                }
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表theater
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            theater model = new theater();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            model.CityId = Convert.ToInt32(this.ddlCity.SelectedValue);
            model.AreaId = Convert.ToInt32(this.ddlCityArea.SelectedValue);

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("theater_Id") != "")
            {
                model.TheaterId = Convert.ToInt32(DNTRequest.GetQueryString("theater_Id").ToString());

                dataResult = new theater_BLL().Update(model);
            }
            else
            {
                dataResult = new theater_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion
        
        #region 加载区域

        /// <summary>
        /// 加载城市的区域列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCityArea.Items.Clear();

            List<movieCityArea> list = new List<movieCityArea>();

            list = new movieCityArea_BLL().GetModelList("CityId=" + this.ddlCity.SelectedValue + " order by AreaOrder");

            if (list != null && list.Count > 0)
            {
                foreach (movieCityArea model in list)
                {
                    ListItem item = new ListItem();

                    item.Text = model.AreaName;
                    item.Value = model.AreaId.ToString();

                    ddlCityArea.Items.Add(item);
                }
            }
        }

        #endregion
    }
}